package config

import (
	"log"

	"github.com/spf13/viper"
)

type ServerConfig struct {
	Address string
	Port    int
	Mode    string
}

type SQLiteConfig struct {
	Path string
}

type MySQLConfig struct {
	Host     string
	Port     int
	User     string
	Password string
	Name     string
}

type PostgresConfig struct {
	Host     string
	Port     int
	User     string
	Password string
	Name     string
	SSLMode  string
}

type DatabaseConfig struct {
	Type     string
	SQLite   SQLiteConfig
	MySQL    MySQLConfig
	Postgres PostgresConfig
}

type RedisConfig struct {
	Use      bool
	Host     string
	Port     int
	Password string
	DB       int
}

type AsynqConfig struct {
	Use bool
}

type LoggerConfig struct {
	Use    bool
	File   string
	Level  uint32
	Format string
}

type Config struct {
	Server   ServerConfig
	Logger   LoggerConfig
	Database DatabaseConfig
	Redis    RedisConfig
	Asynq    AsynqConfig
}

var ConfigInstance *Config

var TemplateDir = "templates"

func InitConfig() {
	viper.SetConfigName("config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath(".")
	viper.AddConfigPath("./config")

	if err := viper.ReadInConfig(); err != nil {
		log.Fatalf("Error reading config file, %s", err)
	}

	err := viper.Unmarshal(&ConfigInstance)
	if err != nil {
		log.Fatalf("Unable to decode into struct, %v", err)
	}
}
